package name.matthewgreet.example3.common; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; import name.matthewgreet.example3.common.Form.Reception; import name.matthewgreet.example3.common.FormStoreInterceptor.StoredForm; /** * <P>Retrieves a form stored in the session by {@link FormStoreInterceptor} and injects it into the action's member * variable configured by the {@link Form} annotation to accept it. It is usually part of the aflViewStack interceptor * stack so a view action can display rejected form data.</P> * * * <H3>Interceptor parameters</H3> * <DL> * <DT>disabled</DT><DD>If true, all processing is disabled. This is useful for standalone popup windows, especially * self-refreshing ones, that never display messages. Defaults to false.</DD> * </DL> * * <H3>Extending the interceptor</H3> * <P>The following method could be overriden :-</P> * <DL> * <DT>fieldReceives</DT><DD>Whether Struts action member variable can be set to the stored form.</DD> * </DL> * * <H3>Example code</H3> * <PRE> * @InterceptorRefs({ * @InterceptorRef(value="formRetrieve"), * @InterceptorRef(value="basicStack") *}) * </PRE> */ public class FormRetrieveInterceptor extends AbstractInterceptor { private static final long serialVersionUID = -6513506646328167140L; private Logger LOG = LogManager.getLogger(FormRetrieveInterceptor.class); private boolean disabled; public FormRetrieveInterceptor() { // Empty } private static Collection<Field> getProperties(Class<?> type) { Collection<Field> fields; Class<?> c; fields = new ArrayList<Field>(); for (c = type; c != null; c = c.getSuperclass()) { fields.addAll(Arrays.asList(c.getDeclaredFields())); } return fields; } private String getFullURL() { if (ServletActionContext.getRequest().getQueryString() != null) { return ServletActionContext.getRequest().getRequestURL().append('?').append(ServletActionContext.getRequest().getQueryString()).toString(); } else { return ServletActionContext.getRequest().getRequestURL().toString(); } } /** * Returns whether Struts action member variable can be set to the stored form, which is whether the member * variable's type is compatible with the form, {@link Form#reception()} annotation settings accept form validation * status, and . */ protected boolean fieldReceives(Class<?> actionClass, Field field, StoredForm storedForm) { Form formAnnotation; Reception reception; Class<?>[] processors; boolean result; if (!field.getType().isAssignableFrom(storedForm.getForm().getClass())) { return false; } if (field.getAnnotation(Form.class) != null) { formAnnotation = field.getAnnotation(Form.class); reception = formAnnotation.reception(); processors = formAnnotation.processors(); } else { reception = Reception.ERROR; processors = new Class<?>[0]; } result = false; switch (reception) { case NEVER: result = false; break; case ERROR: result = storedForm.getInvalid(); break; case SUCCESS: result = !storedForm.getInvalid(); break; case ALWAYS: result = true; break; } if (result && processors.length > 0) { result = false; for (Class<?> processor: processors) { if (processor.isAssignableFrom(actionClass)) { result = true; break; } } } return result; } public boolean getDisabled() { return disabled; } public void setDisabled(boolean value) { disabled = value; } @Override public String intercept(ActionInvocation invocation) throws Exception { StoredForm storedForm; Collection<Field> fields; Object rawObject; boolean receive; if (!disabled) { rawObject = ActionContext.getContext().getSession().get(FormStoreInterceptor.SESSION_STORED_FORM); if (rawObject != null) { if (rawObject instanceof StoredForm) { storedForm = (StoredForm)rawObject; if (storedForm.getOwningURL() == null || storedForm.getOwningURL().equals(getFullURL())) { fields = getProperties(invocation.getAction().getClass()); for (Field field: fields) { receive = fieldReceives(invocation.getAction().getClass(), field, storedForm); if (receive) { try { field.setAccessible(true); field.set(invocation.getAction(), storedForm.getForm()); } catch (Exception e) { LOG.error("Set stored form of Struts action member variable failed" + " Struts action=" + invocation.getAction().getClass() + " field=" + field.getName(), e); } } } storedForm.setOwningURL(getFullURL()); } else { // Remove as user is displaying another page ActionContext.getContext().getSession().remove(FormStoreInterceptor.SESSION_STORED_FORM); } } else { LOG.warn("Session attribute is not a stored form session attribute=" + FormStoreInterceptor.SESSION_STORED_FORM + " value=" + rawObject); } } } return invocation.invoke(); } }